package com.ruoyi.project.module.wyw;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.domain.YqgHands;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import com.ruoyi.project.service.IYqgHandsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 手牌基础信息Controller
 * 
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/wywhands")
public class WywYqgHandsController extends BaseController {
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;

    /**
     * 查询手牌基础信息列表
     */
//    @RequiresPermissions("project:hands:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgHands yqgHands) {
        startPage();
        List<YqgHands> list = yqgHandsService.selectYqgHandsList(yqgHands);
        return getDataTable(list);
    }

    /**
     * 导出手牌基础信息列表
     */
//    @RequiresPermissions("project:hands:export")
    @Log(title = "手牌基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgHands yqgHands) {
        List<YqgHands> list = yqgHandsService.selectYqgHandsList(yqgHands);
        ExcelUtil<YqgHands> util = new ExcelUtil<YqgHands>(YqgHands.class);
        util.exportExcel(response, list, "手牌基础信息数据");
    }

    /**
     * 获取手牌基础信息详细信息
     */
//    @RequiresPermissions("project:hands:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(yqgHandsService.selectYqgHandsById(id));
    }

    /**
     * 新增手牌基础信息
     */
//    @RequiresPermissions("project:hands:add")
    @Log(title = "手牌基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgHands yqgHands) {
        return toAjax(yqgHandsService.insertYqgHands(yqgHands));
    }

    /**
     * 修改手牌基础信息
     */
//    @RequiresPermissions("project:hands:edit")
    @Log(title = "手牌基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgHands yqgHands) {

        return toAjax(yqgHandsService.updateYqgHands(yqgHands));
    }

    /**
     * 删除手牌基础信息
     */
//    @RequiresPermissions("project:hands:remove")
    @Log(title = "手牌基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yqgHandsService.deleteYqgHandsByIds(ids));
    }


    /**
     * 锁牌
     */
    //@RequiresPermissions("project:hands:edit")
    @Log(title = "解/锁牌", businessType = BusinessType.UNLOCK, logicalKey = "handBrand")
    @PutMapping("/suopai")
    public AjaxResult suopai(@RequestBody YqgHands yqgHands) {
        //需求：我们要通过前台传递过来的手牌号，去查这个手牌的锁定状态，然后判断，如果锁定了就解锁，反之开锁

        //代码解析
        //通过手牌号查询这个手牌号的所有信息
        //yqgHands是前台传过来的实体，通过yqgHandsService服务层的selectYqgHandsList方法进行查询，本质就是执行mapper层的一条sql
        //将查询结果返回给list
        List<YqgHands> list = yqgHandsService.selectYqgHandsList(yqgHands);
        //判断锁定状态，因为list是一个列表，因为手牌号是唯一的，所以要取list第一条数据：list.get(0)
        //如果锁定状态是锁定（0），那么就修改为解锁（1）
        //如果锁定状态是解锁（1），那么就修改为锁定（0）
        if (list.get(0).getLockedState().equals("0")) {
            //设置锁定状态
            list.get(0).setLockedState("1");
            //执行更新方法
            yqgHandsService.updateYqgHands(list.get(0));
        } else {
            //设置锁定状态
            list.get(0).setLockedState("0");
            //执行更新方法
            yqgHandsService.updateYqgHands(list.get(0));
        }
        return success();
    }


    @PutMapping("/checkhuanpai")
    public AjaxResult checkhuanpai(@RequestBody YqgHands yqgHands) {

        //先判断

        YqgHands yqgHands1 = new YqgHands();
        yqgHands1.setHandBrand(yqgHands.getHandBrand());
        List<YqgHands> qianlist = yqgHandsService.selectYqgHandsList(yqgHands1);
        if (qianlist.isEmpty()) {
            return new AjaxResult(500, "请输入正确的原手牌号");
        }else if(qianlist.get(0).getHandState().equals("0")){
            return new AjaxResult(500, "原手牌号未开牌，请重新输入");
        }

        YqgHands yqgHands2 = new YqgHands();
        yqgHands2.setHandBrand(yqgHands.getNote5());
        System.out.println(yqgHands.getNote5()+"note5");
        List<YqgHands> houlist = yqgHandsService.selectYqgHandsList(yqgHands2);
        System.out.println(houlist+"houlist");
        if (houlist.isEmpty()) {
            return new AjaxResult(500, "请输入正确的新手牌号");
        }
        if (houlist.get(0).getHandState().equals("1")) {
            return new AjaxResult(500, "新手牌号已在台，请重新输入");
        }





        return success();
    }

    /**
     * 换牌
     */
    //@RequiresPermissions("project:hands:edit")
    @Log(title="换牌",businessType = BusinessType.CHANGE,logicalKey = "note5")
    @PutMapping("/huanpai")
    public AjaxResult huanpai(@RequestBody YqgHands yqgHands)
    {

        //先判断






        //需求：前台传过来一个原手牌号和新手牌号
        //新手牌号的操作：1.在手牌基础信息表把这个手牌的手牌状态改成在台
        //               2.在吧台账单表把原手牌字段写为原手牌号,手牌号变成新手牌号
        //               3.把详细消费表所有原手牌的记录的手牌号改成新手牌号
        //原手牌的操作：1.把手牌基础信息表的手牌状态变成空牌

        //1.在手牌基础信息表把这个手牌的手牌状态改成在台
        //创建一个YqgHands对象
        YqgHands xinyqgHands=new YqgHands();
        //把xinyqgHands的手牌号设置为前台传过来的新手牌号
        xinyqgHands.setHandBrand(yqgHands.getNote5());
        //执行查询，查询手牌号是新手牌号的那条数据，返回给list
        List<YqgHands> list = yqgHandsService.selectYqgHandsList(xinyqgHands);


        //将这条数据的手牌状态设置为在台
        list.get(0).setHandState("1");
        //执行更新操作
        yqgHandsService.updateYqgHands(list.get(0));





        //2.在吧台账单表把原手牌字段写为原手牌号,手牌号变成新手牌号
        //创建一个YqgHands对象
        YqgHands yuanyqgHands=new YqgHands();
        //把yuanyqgHands的手牌号设置为前台传过来的原手牌号
        yuanyqgHands.setHandBrand(yqgHands.getHandBrand());

        //创建一个YqgBarBills对象，吧台账单表
        YqgBarBills yuanyqgBarBills=new YqgBarBills();
        //把yuanyqgBarBills的手牌号设置为前台传过来的原手牌号
        yuanyqgBarBills.setHandBrand(yqgHands.getHandBrand());
        //把yuanyqgBarBills的手牌状态设置为在台
        yuanyqgBarBills.setHandState("1");
        //执行查询，通过原手牌号查询出他在YqgBarBills表的数据，返回给yuanBillslist
        List<YqgBarBills> yuanBillslist = yqgBarBillsService.selectYqgBarBillsList(yuanyqgBarBills);
        //因为账单信息表只会有一条数据，所以使用yuanBillslist.get(0)

        //将这条数据的原手牌号设置为yqgHands.getNote5()，就是新手牌号
        yuanBillslist.get(0).setHandBrand(yqgHands.getNote5());
        //执行更新操作
        yqgBarBillsService.updateYqgBarBills(yuanBillslist.get(0));

        YqgBarBills yqgBarBills=new YqgBarBills();
        yqgBarBills.setHandBrand(yqgHands.getNote5());
        yqgBarBills.setHandState("1");
        List<YqgBarBills> list1 = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);

        //3.把详细消费表所有原手牌的记录的手牌号改成新手牌号
        //创建一个YqgConsumptionDetails对象，详细消费表
        YqgConsumptionDetails yuanyqgConsumptionDetails=new YqgConsumptionDetails();
        //把yuanyqgConsumptionDetails的手牌号设置为前台传过来的原手牌号
        yuanyqgConsumptionDetails.setHandBrand(yqgHands.getHandBrand());
        //把yuanyqgConsumptionDetails的手牌状态设置为在台
        yuanyqgConsumptionDetails.setHandState("1");
        //执行查询，通过原手牌号查询出他在YqgConsumptionDetails表的数据，返回给yuanConsumDetail
        List<YqgConsumptionDetails> yuanConsumDetail = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yuanyqgConsumptionDetails);
        //因为详细消费表有多条消费记录，所以使用循环得出每一台记录，然后进行修改
        for(YqgConsumptionDetails yuanc:yuanConsumDetail){
        //把手牌号设置为新手牌号
            yuanc.setHandBrand(yqgHands.getNote5());
            yuanc.setBarBillsId(list1.get(0).getID());
        //执行更新操作
            yqgConsumptionDetailsService.updateYqgConsumptionDetails(yuanc);
        }










        //原手牌的操作：1.把手牌基础信息表的手牌状态变成空牌
        //创建一个YqgHands对象
        YqgHands yuanyqgHandstwo=new YqgHands();
        //把手牌号设置为yqgHands.getHandBrand()
        yuanyqgHandstwo.setHandBrand(yqgHands.getHandBrand());
        //执行查询
        List<YqgHands> yuanlist = yqgHandsService.selectYqgHandsList(yuanyqgHandstwo);
        //把查出来的数据的手牌状态设置为空牌
        yuanlist.get(0).setHandState("0");
        //执行更新操作
        yqgHandsService.updateYqgHands(yuanlist.get(0));
        return success();
    }




}
